In [16]:
%matplotlib inline
import nibabel as nib
from nilearn import plotting as niplt
import numpy as np

Here, I'm going to create the mask that defined MFC for further analysis.

First, I load a cerebral cortex probabilty map, from the Harvard Oxford atlas


In [17]:
cortex = nib.load('cerbcort.nii.gz')

In [18]:
# Binarize
cortex = nib.Nifti1Image((cortex.get_data() > 0).astype('int'), cortex.get_header().get_best_affine())
niplt.plot_roi(cortex)


Out[18]:
<nilearn.plotting.displays.OrthoSlicer at 0x115d05fd0>

Next, I use meshgrid to mask voxels based on location. Importantly, we use nibabel's affine to convert an MNI coordinate (e.g. 10, 0, 0), to voxel space.


In [19]:
i, j, k = np.meshgrid(*map(np.arange, cortex.get_data().shape), indexing='ij')

# Maximum left and right X coordinates
X_l = nib.affines.apply_affine(np.linalg.inv(cortex.get_affine()), [-10, 0, 0])[0]
X_r = nib.affines.apply_affine(np.linalg.inv(cortex.get_affine()), [10, 0, 0])[0]

# Maximum Y and Z coordinates
Y = nib.affines.apply_affine(np.linalg.inv(cortex.get_affine()), [0, -22, 0])[1]
Z = nib.affines.apply_affine(np.linalg.inv(cortex.get_affine()), [0, 0, -32])[2]

Finally, we use the voxel space coordinates to mask the 30% Harvard-Oxford cortical mask, and binarize it


In [21]:
## Exclude lateral 
cortex.get_data()[
    np.where((i < X_r) | 
             (i > X_l))] = 0

# Exclude posterior
cortex.get_data()[
    np.where(j < Y)] = 0

## Exclude ventral 
cortex.get_data()[
    np.where(k < Z)] = 0

# Binarize
cortex.get_data()[cortex.get_data() < 1] = 0
cortex.get_data()[cortex.get_data() >= 1] = 1

In [22]:
niplt.plot_roi(cortex)


Out[22]:
<nilearn.plotting.displays.OrthoSlicer at 0x1166f3a50>

In [ ]: